package com.jcfk.eam.service.main;

import cn.hutool.core.util.StrUtil;
import com.jcfk.eam.domain.dto.base.EamSopItemDTO;
import com.jcfk.eam.domain.dto.base.EamSopStandardDTO;
import com.jcfk.eam.domain.dto.main.EamAssetInfoDTO;
import com.jcfk.eam.domain.dto.main.EamPointCheckDetailDTO;
import com.jcfk.eam.domain.dto.main.EamPointCheckOrderDTO;
import com.jcfk.eam.domain.dto.main.EamPointCheckResultDTO;
import com.jcfk.eam.domain.query.base.EamSopStandardQuery;
import com.jcfk.eam.service.base.EamSopStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 资产点检
 */
@Service
public class EamPointInspectionService {

    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    @Autowired
    private EamSopStandardService eamSopStandardService;

    /**
     * 根据资产获取标准作业列表
     * @param query
     */
    public List<EamSopStandardDTO> getSopStandardList(EamSopStandardQuery query){
        String assetTypeId="";
        if(StrUtil.isNotEmpty(query.getAssetId())){
            EamAssetInfoDTO eamAssetInfo=eamAssetInfoService.get(query.getAssetId());
            assetTypeId=eamAssetInfo.getAssetTypeId();
        }
        if(StrUtil.isNotEmpty(query.getEquipTypeId())){
            assetTypeId=query.getEquipTypeId();
        }
        List<EamSopStandardDTO> list=eamSopStandardService.getStandardList(query.getAssetKind(),query.getTaskKind(),assetTypeId);

        return list;
    }

    /**
     * 新建点检单
     * @param assetId
     * @param sopStandard
     * @return
     */
    public EamPointCheckOrderDTO newPointCheckOrder(EamSopStandardDTO sopStandard){
        EamPointCheckResultDTO eamPointCheckResultDTO= eamSopStandardService.getSopStandard(sopStandard.getSopTypeCode(),sopStandard.getStandardCode());

        EamPointCheckOrderDTO pointCheckOrderDTO=new EamPointCheckOrderDTO();
        pointCheckOrderDTO.setSopTypeId(eamPointCheckResultDTO.getSopTypeId());
        pointCheckOrderDTO.setSopTypeCode(eamPointCheckResultDTO.getSopTypeCode());
        pointCheckOrderDTO.setSopTypeName(eamPointCheckResultDTO.getSopTypeCode());
        pointCheckOrderDTO.setSopStandardId(sopStandard.getId());
        pointCheckOrderDTO.setSopStandardCode(sopStandard.getStandardCode());
        pointCheckOrderDTO.setSopStandardName(sopStandard.getStandardName());
        pointCheckOrderDTO.setOrderStatus(0);
        pointCheckOrderDTO.setDetails(new ArrayList<>());

        for(EamSopItemDTO sopItem : eamPointCheckResultDTO.getItemDTOS()){
            EamPointCheckDetailDTO checkDetail=new EamPointCheckDetailDTO();
            checkDetail.setSopItemId(sopItem.getId());
            checkDetail.setSopItemName(sopItem.getSopItemName());
            checkDetail.setSopItem(sopItem);
            if(sopItem.getStandardValue().equals("1")) {
                //测量项
                if (StrUtil.isNotEmpty(sopItem.getStandardMinValue()) && StrUtil.isNotEmpty(sopItem.getStandardMaxValue())) {
                    checkDetail.setSopItemStandard(sopItem.getStandardMinValue() + "-" + sopItem.getStandardMaxValue());
                }
                if(StrUtil.isNotEmpty(sopItem.getStandardValueUnit())){
                    checkDetail.setSopItemStandard(checkDetail.getSopItemStandard()+sopItem.getStandardValueUnit());
                }
            }
            pointCheckOrderDTO.getDetails().add(checkDetail);
        }

        return pointCheckOrderDTO;
    }


}
